<template> 
    <div>
        <Form ref="formValidate" :model="formValidate" :label-width="100" :rules="ruleValidate">
            <FormItem label="学生" prop="user_id">
                <div @click="infoId==false ?  othermodule=true : ''">
                    <RadioGroup v-model="formValidate.user_id" type="button" size="large">
                        <Radio :label="formValidate.user_id">{{username.zh?username.zh:username.mn}}</Radio>
                    </RadioGroup>
                    <span v-if="infoId==false" style="color:#ccc">请选择学生</span>
                </div>
            </FormItem>
         
            <FormItem label="异动类型" prop="transaction_type">
                <Select v-model="formValidate.transaction_type">
                    <Option  v-for="item in listDictionary" :value="item.dictionary_id" :key="item.dictionary_id">{{ item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn }}</Option>
                </Select>
            </FormItem>
            <FormItem label="异动时间" prop="eclass_time">
                <DatePicker v-model="formValidate.eclass_time"  type="date"  placeholder="请选择异动时间" style="width:100%"></DatePicker>
            </FormItem>
            <FormItem label="异动原因" prop="reason.zh">
                <Input v-model="formValidate.reason.zh" placeholder="异动原因"></Input>
            </FormItem>

            <div style="width:90%;">
                <FormItem style="width:100%;">
                    <Button type="primary" @click="handleSubmit('formValidate')">保存</Button>
                </FormItem>
            </div>
            <div style="clear:both"></div>
            <Spin size="large" fix v-if="spinShow"></Spin>
        </Form> 
        <Modal v-model="othermodule" title="请选择学生" width="350">
            <student-module @on-select="othermodulebutton"></student-module>
        </Modal> 

    </div>
</template>
<script>
  export default {
    props: {
        infoId: {
            type: [Boolean,Number,String],
            default: false
        }
    },
    components: {	
        StudentModule: resolve => {require(['&/Authpub/StudentModule.vue'], resolve);},

    },
    data() {
        return {
            infoUrl:`api/student/v1/studentlearnstatus/changes_lists/${this.infoId}`,//请求单条数据
            saveUrl:'',//保存接口
            saveType:null,//保存方式

            othermodule:false,
            username:'请选择学生',

            spinShow:false,//加载动画

            listDictionary: [],//数据字典  入选等级
            infoOld:[],//单挑请求的数据

            formValidate: {
                user_id:null,//学生id
                transaction_type:null,//异动状态
                eclass_time:null,//异动时间
                reason:{
                    zh:'',
                    mn:'',
                    en:'',
                },//异动原因       
            },
            ruleValidate: {
                'reason.zh': [
                    { required: true, message: '异动原因不得为空', trigger: 'blur' }
                ],
                transaction_type: [
                    { type:'number',required: true, message: '异动状态必须选', trigger: 'change' }
                ],
                eclass_time: [
                    { required: true, type: 'date', message: '请选择异动时间', trigger: 'blur' }
                ],
            
            },
        }
    },
    methods: {

        /************************************************************
         * 
         * 提交时
         * 
         ************************************************************/
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {    

                    this.spinShow=true//输入框禁用     
                    this.formValidate.eclass_time= this.transDate(this.formValidate.eclass_time)
                    //console.log(this.formValidate.eclass_time,this.transDate(this.formValidate.eclass_time))
                    this.saveType(this.saveUrl,this.formValidate).then(val=>{
                        this.$emit('showParent',false)
                        this.$emit('backList',true)
                        this.$Message.success(val.message);
                        this.spinShow=false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.spinShow=false
                    })
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        
        /************************************************************
         * 
         * 单条数据请求
         * 
         ************************************************************/
        infoData(){
            this.$http.get(this.infoUrl).then(val=>{   
                delete this.formValidate.user_id 
                this.infoOld = JSON.parse(JSON.stringify(val.data))              
                this.username=this.infoOld.user ? this.infoOld.user.student_name : '请选择学生'
                this.formValidate.eclass_time = this.infoOld.eclass_time//异动时间
                if(this.infoOld.transaction_type!=null){
                    this.formValidate.transaction_type = parseInt(this.infoOld.transaction_type.dictionary_id)//异动状态
                }


                this.formValidate.reason.zh = this.infoOld.reason?this.infoOld.reason.zh:''//异动原因

                this.spinShow=false//输入框禁用          
            }).catch(err=>{
                console.log(err);
                if(err.code && err.code==422) {
                     this.$Message.error(err.message);
                }
                this.spinShow = false;
            })
        },
            
        /************************************************************
         * 
         * 点击班级执行
         * 
         ************************************************************/
        othermodulebutton(val){
            this.formValidate.user_id=val.value.toString()
            this.username=val.lable
            this.othermodule=false
        },
    },
    created() {
        this.spinShow=true
        /************************************************************
         * 
         * 数据字典获取数据
         * 
         ************************************************************/
        this.$http.get('api/dictionary/v1/dictionary/pidquerys?transaction_type')
            .then(val=>{   
                this.listDictionary= val.data.transaction_type.filter(item => item.dictionary_id != 360)
                this.spinShow=false          
            })
            .catch(err=>{
                console.log(err);
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
                this.spinShow = false;
            })

        if(this.infoId==false){
            // add
            this.saveUrl='api/student/v1/studentlearnstatus/student_changes'
            this.saveType=this.$http.post

        }else{
            // edit
            this.infoData();
            this.saveUrl=`api/student/v1/studentlearnstatus/changes_update/${this.infoId}`
            this.saveType=this.$http.put
        }
    },
  }
</script>